package com.googlecode.dgwt.client.dijit.base;

import com.googlecode.dgwt.client.dijit.Widget;
import com.googlecode.dgwt.client.dojo.Dojo;
import com.googlecode.dgwt.client.dojo.core.JsMap;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public abstract class WidgetProperty {

    /**
     * Constant to identify a getter/setter for DOM element properties
     */
    public static final WidgetProperty DOM_PROPERTY = new WidgetProperty(){

        @Override
        public Object get(Widget widget, String propertyName) {
            String elementName = widget.getElementForAttribute(propertyName);
            return widget.getAttachedElement(elementName).<JsMap<Object>>cast().get(propertyName);
        }

        @Override
        public void set(Widget widget, String propertyName, Object propertyValue) {
            String elementName = widget.getElementForAttribute(propertyName);
            widget.getAttachedElement(elementName).<JsMap<Object>>cast().set(propertyName, propertyValue);
        }
        
    };
    
    /**
     * Constant to identify a getter/setter for style properties
     */
    public static final WidgetProperty STYLE_PROPERTY = new WidgetProperty(){

        @Override
        public Object get(Widget widget, String propertyName) {
            String elementName = widget.getElementForAttribute("style." + propertyName);
            if (elementName == null){
                elementName = "styleElement";
            }
            return Dojo.getStyle(widget.getAttachedElement(elementName), propertyName);
        }

        @Override
        public void set(Widget widget, String propertyName, Object propertyValue) {
            String elementName = widget.getElementForAttribute("style." + propertyName);
            if (elementName == null){
                elementName = "styleElement";
            }
            Dojo.setStyle(widget.getAttachedElement(elementName), propertyName, 
                    propertyValue == null ? "" : "" + propertyValue);
        }
        
    };
    
    /**
     * Constant to identify a getter/setter for element attributes
     */
    public static final WidgetProperty ATTRIBUTE_PROPERTY = new WidgetProperty(){

        @Override
        public Object get(Widget widget, String propertyName) {
            String elementName = widget.getElementForAttribute(propertyName);
            return widget.getAttachedElement(elementName).getAttribute(propertyName);
        }

        @Override
        public void set(Widget widget, String propertyName, Object propertyValue) {
            String elementName = widget.getElementForAttribute(propertyName);
            widget.getAttachedElement(elementName).setAttribute(propertyName, 
                    propertyValue == null ? "" : "" + propertyValue);
        }
        
    };

    protected WidgetProperty(){        
    }
    
    public abstract Object get(Widget widget, String propertyName);
    
    public abstract void set(Widget widget, String propertyName, Object propertyValue);
    
}
